package com.zzhrtech.wgzx_cityzen.ui.task;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.android.volley.Request;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.GsonRequest;
import com.zzhrtech.WgzxApplication;
import com.zzhrtech.wgzx_cityzen.BaseActivity;
import com.zzhrtech.wgzx_cityzen.Constans;
import com.zzhrtech.wgzx_cityzen.R;
import com.zzhrtech.wgzx_cityzen.adapters.task.PhoneListAdapter;
import com.zzhrtech.wgzx_cityzen.base.OnItemClickLitener;
import com.zzhrtech.wgzx_cityzen.beans.task.PhoneListBean;

import java.util.List;


public class CommonPhoneActivity extends BaseActivity {

    RecyclerView rv_phone;
    PhoneListAdapter listAdapter;
    private List<PhoneListBean.Data> listBeanList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_common_phone);

        toolbar.setNavigationIcon(R.mipmap.ic_close);
        initView();

        httpGetBuilder();
    }

    private void initView(){
        rv_phone = (RecyclerView) findViewById(R.id.rv_phone);
        rv_phone.setLayoutManager(new LinearLayoutManager(this));
        rv_phone.setItemAnimator(new DefaultItemAnimator());
        listAdapter = new PhoneListAdapter();
        rv_phone.setAdapter(listAdapter);

        listAdapter.setOnItemClickLitener(new OnItemClickLitener() {
            @Override
            public void onItemClick(View view, final int position) {
//                Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + listBeanList.get(position).getB_phone()));
//                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//                startActivity(intent);

                new MaterialDialog.Builder(CommonPhoneActivity.this)
                        .title("确定派修")
                        .content("请确定已经联系施工单位")
                        .negativeText("取消")
                        .positiveText("确定")
                        .onPositive(new MaterialDialog.SingleButtonCallback() {
                            @Override
                            public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                                Intent intent = new Intent();
                                intent.putExtra("bid",listBeanList.get(position).getB_id());
                                setResult(RESULT_OK,intent);
                                finish();
                                dialog.dismiss();
                            }
                        })
                        .show();

            }
        });

    }

    private Handler mHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {

            listBeanList = (List<PhoneListBean.Data>) msg.obj;
            for (PhoneListBean.Data bean : listBeanList){
                listAdapter.addItem(bean);
            }

            return false;
        }
    });


    private void httpGetBuilder(){

        GsonRequest<PhoneListBean> gsonRequest = new GsonRequest<PhoneListBean>(Request.Method.GET, Constans.HTTP_BULDERLIST, PhoneListBean.class, null, new com.android.volley.Response.Listener<PhoneListBean>() {
            @Override
            public void onResponse(PhoneListBean response) {

                if (response.isSuccess()){
                    Message message = new Message();
                    message.what = 0;
                    message.obj = response.getData();
                    mHandler.sendMessage(message);
                }else {
                    showToast(response.getMsg());
//                    mDialog.dismiss();
                }
            }
        }, new com.android.volley.Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.d("result=====",error + "");
//                mDialog.dismiss();
            }
        });

        WgzxApplication.addRequest(gsonRequest, Constans.reqTag);
    }
}
